package org.demo.ftp;

import java.text.NumberFormat;
import java.util.concurrent.BlockingQueue;

/**
 * 
 * @author  
 * @date    2011-1-23
 * @file    org.demo.ftp.UpdateUITask.java
 */
public class UpdateUITask implements Runnable {
	
	// 将上传/下载进度格式为百分比
	private NumberFormat format = NumberFormat.getPercentInstance();
	// 状态信息
	private BlockingQueue<StatusInfo> queue = null;
	
	/**
	 * 构造函数
	 * @param queue
	 */
	public UpdateUITask(BlockingQueue<StatusInfo> queue){
		this.queue = queue;
		format.setMaximumFractionDigits(2);
		format.setMinimumFractionDigits(2);
	}
	@Override
	public void run() {
		float value = 1.0f;
		String percent = "";
		while(true){
			try {
				StatusInfo info = queue.take();
				value = (info.getCompleteSize() * 1.0f) / info.getCurrentFileSize();
				percent = format.format(value);
				if (info.isDownload()){
					System.out.println(">> download percent : " + percent + " [" + info.getRemoteFileName() + "]");
				} else {
					System.out.println(">>   upload percent : " + percent + " [" + info.getLocalFileName() + "]");
				}
			} catch (Exception e){
				// Ignore.
			}
		}
	}
}
